package cn.zzu.Etrip.bestselect.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.administrator.etrip.R;

import java.util.ArrayList;
import java.util.HashMap;

import cn.zzu.Etrip.localbest.service.LocalBestService;

/**
 * Created by ASUS on 2018/5/23.
 */

public class BestSelectActivity extends AppCompatActivity {
    private ListView listView;
    private Button buttonbendi, buttonzhoubian, buttonyuanfang, buttontuijian, buttonqinglv;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bestselect);
        //定义一个动态的数组
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

        listView = findViewById(R.id.localbest_listView);
        buttonbendi = findViewById(R.id.bendi);
        buttonzhoubian = findViewById(R.id.zhuobian);
        buttonyuanfang = findViewById(R.id.yuanfang);
        buttontuijian = findViewById(R.id.tuijian);
        buttonqinglv = findViewById(R.id.qinglv);

        //为每一个按钮添加一个监听器
        buttonbendi.setOnClickListener(new cn.zzu.Etrip.bestselect.activity.BestSelectActivity.ButtonListener(R.id.bendi, list, listView, LocalBestService.bendiimageId));
        buttonzhoubian.setOnClickListener(new cn.zzu.Etrip.bestselect.activity.BestSelectActivity.ButtonListener(R.id.zhuobian, list, listView,LocalBestService.zhuobianImageId));
        buttonyuanfang.setOnClickListener(new cn.zzu.Etrip.bestselect.activity.BestSelectActivity.ButtonListener(R.id.yuanfang, list, listView,LocalBestService.dangjiImageId));
        buttontuijian.setOnClickListener(new cn.zzu.Etrip.bestselect.activity.BestSelectActivity.ButtonListener(R.id.tuijian, list, listView,LocalBestService.zhuobianImageId));
        buttonqinglv.setOnClickListener(new cn.zzu.Etrip.bestselect.activity.BestSelectActivity.ButtonListener(R.id.qinglv, list, listView,LocalBestService.dangjiImageId));

        //建立适配器
        for (int i = 0; i < 5; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", LocalBestService.bendiimageId[i]);
            map.put("ItemTitle", LocalBestService.bendiimageInfo[i]);
            list.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(cn.zzu.Etrip.bestselect.activity.BestSelectActivity
                .this, list, R.layout.layout_listview_item, new String[]{"ItemImage", "ItemTitle"}, new int[]{R.id.localbest_ItemImage, R.id.localbest_ItemText});
        listView.setAdapter(simpleAdapter);
        //为listView绑定一个适配器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.localBestPayActivity");
                Bundle bundle = new Bundle();
                bundle.putInt("id",LocalBestService.bendiimageId[position]);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    //定义按钮组监听器
    class ButtonListener implements View.OnClickListener {
        private int a;
        private ArrayList<HashMap<String, Object>> list;
        private ListView listView;
        private int []imageid;
        ///构造方法给以上参数进行赋值
        public ButtonListener(int a, ArrayList<HashMap<String, Object>> list, ListView listView,int []imageid) {
            this.a = a;
            this.list = list;
            this.listView = listView;
            this.imageid=imageid;
        }

        public void onClick(View v) {
            switch (a) {
                case R.id.bendi:
                    setAdapter(imageid, LocalBestService.bendiimageInfo, listView);
                    break;
                case R.id.zhuobian:
                    setAdapter(imageid, LocalBestService.bendiimageInfo, listView);
                    break;
                case R.id.yuanfang:
                    setAdapter(imageid, LocalBestService.bendiimageInfo, listView);
                    break;
                case R.id.tuijian:
                    setAdapter(imageid, LocalBestService.bendiimageInfo, listView);
                    break;
                case R.id.qinglv:
                    setAdapter(imageid, LocalBestService.bendiimageInfo, listView);
                    break;
            }
        }

        public void setAdapter(int[] imageId, String[] imageInfo, ListView listView) {

            //主要的代码，将以前的 数据进行清除，然后在添加新的内容***************************************************
            list.clear();
            for (int i = 0; i < 5; i++) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("ItemImage", imageId[i]);
                map.put("ItemTitle", imageInfo[i]);
                list.add(map);
            }
            //建立一个适配器
            SimpleAdapter simpleAdapter2;
            simpleAdapter2 = new SimpleAdapter(cn.zzu.Etrip.bestselect.activity.BestSelectActivity
                    .this, list, R.layout.layout_listview_item, new String[]{"ItemImage", "ItemTitle"}, new int[]{R.id.localbest_ItemImage, R.id.localbest_ItemText});
            listView.setAdapter(simpleAdapter2);
            //为listView绑定一个适配器
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.localBestPayActivity");
                    Bundle bundle = new Bundle();
                    bundle.putInt("id",imageid[position]);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
        }

    }
}
